package com.qf.service.impl;

import com.qf.dao.QfCourseDao;
import com.qf.pojo.RedisKey;
import com.qf.pojo.reqVo.CourseReq;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.QfCourse;
import com.qf.service.ShopCardService;
import com.qf.utils.JwtUtils;
import com.qf.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class ShopCardServiceImpl implements ShopCardService {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    QfCourseDao qfCourseDao;

    @Autowired
    UserUtils utils;

    @Override
    public BaseResp addShopCard(CourseReq courseReq, HttpServletRequest httpServletRequest) {
        //1.解析 http获取到用户信息
        Integer userId = utils.getUserId(httpServletRequest);
        if (userId==0){
            return new BaseResp(0,"添加失败！",null,null);
        }
        //1.判断购物车是否存在
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCARD_ + userId.toString());
        if (aBoolean) {
            //判断当前课程是否已经存在于购物车
            Object o = redisTemplate.opsForHash().get(RedisKey.SHOPCARD_ + userId.toString(), courseReq.getCourseid().toString());
            if (o != null) {
                return new BaseResp(0, "该课程已经存在！不允许重复添加！", null, null);
            }
        }
            //如果没有该课程则直接添加到redis中,我们从course_deatil_list的数据中获取到该课程的信息
            Object o1 = redisTemplate.opsForHash().get(RedisKey.COURSEDETAILLIST, courseReq.getCourseid().toString());
            if (o1==null){
                //从数据库中查询，放置到redis中的购物车
                QfCourse qfcourse = qfCourseDao.findCourseDetailById(courseReq.getCourseid());
                //放置到reids中的购物车
                redisTemplate.opsForHash().put(RedisKey.SHOPCARD_+userId.toString(),courseReq.getCourseid().toString(),qfcourse);
            }else{
                redisTemplate.opsForHash().put(RedisKey.SHOPCARD_+userId.toString(),courseReq.getCourseid().toString(),o1);
            }

        return new BaseResp(1,"添加成功！",null,null);
    }

    @Override
    public BaseResp findShopCardByToken(HttpServletRequest request) {
        //1.解析出用户的id值
        Integer userId = utils.getUserId(request);
        if (userId==0){
            return new BaseResp(0,"用户未登录！",null,null);
        }
        //从redis中获取该用户的购物车
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCARD_ + userId.toString());
        if (!aBoolean){
            return new BaseResp(0,"用户未添加课程！",null,null);
        }
        Map entries = redisTemplate.opsForHash().entries(RedisKey.SHOPCARD_ + userId.toString());
        return new BaseResp(1,"查询成功！",entries,null);
    }

    @Override
    public BaseResp delShop(CourseReq courseReq, HttpServletRequest request) {
        //1.解析出用户的id值
        Integer userId = utils.getUserId(request);
        if (userId==0){
            return new BaseResp(0,"用户未登录！",null,null);
        }
        //判断是否有该购物车
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCARD_ + userId.toString());
        if (!aBoolean){
            return new BaseResp(0,"购物车五商品！",null,null);
        }
        Long size = redisTemplate.opsForHash().size(RedisKey.SHOPCARD_ + userId.toString());
        if (size>1){
                 redisTemplate.opsForHash().delete(RedisKey.SHOPCARD_+userId.toString(),courseReq.getCourseid().toString());

        }else{
            redisTemplate.delete(RedisKey.SHOPCARD_+userId.toString());
        }

        return new BaseResp(1,"删除成功！",null,null);
    }


}
